﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// 植物格子管理，（坐标都是世界坐标）
public class GridMgr
{
    Transform _sceneRoot;

    Vector3 _leftBottom = Vector3.zero;
    Vector3 _rightTop = Vector3.zero;

    int _row; // 行
    int _colum;

    float _gridWidth = 0f; // 格子宽
    float _gridHeight = 0f;

    float _halfWidth; // 格子半宽
    float _halfHeight;

    Grid[][] _allGrids; // 所有格子(x, y)坐标

    public GridMgr()
    {
        GizmosMgr.instance.OnGizmos += onGizmos;
    }

    // 格子划分信息
    public void SetGridInfo(Vector3 leftBottom, Vector3 rightTop, int row, int colum, Transform sceneRoot)
    {
        _sceneRoot = sceneRoot;

        _leftBottom = leftBottom;
        _rightTop = rightTop;

        _row = row;
        _colum = colum;

        var totalWidth = _rightTop.x - _leftBottom.x;
        var totalHeight = _rightTop.y - _leftBottom.y;

        _gridWidth = totalWidth / colum;
        _gridHeight = totalHeight / row;

        _halfWidth = _gridWidth * 0.5f;
        _halfHeight = _gridHeight * 0.5f;

        // 生成格子
        _allGrids = new Grid[colum][];
        for (int x = 0; x < colum; x++)
        {
            _allGrids[x] = new Grid[row];

            for (int y = 0; y < row; y++)
            {
                _allGrids[x][y] = new Grid(this, x, y);
            }
        }
    }

    // 获取格子坐标(左下角是0，0点)。
    public Vector2 GetGridPos(Vector3 worldPos)
    {
        // 格子坐标系归零
        var posInGrid = worldPos - _leftBottom;

        var x = Mathf.FloorToInt(posInGrid.x / _gridWidth);
        var y = Mathf.FloorToInt(posInGrid.y / _gridHeight);

        return new Vector2(x, y);
    }

    public Grid GetGrid(Vector3 worldPos)
    {
        var pos = GetGridPos(worldPos);
        return GetGrid(pos);
    }
    public Grid GetGrid(Vector2 gridPos)
    {
        return GetGrid((int)gridPos.x,((int)gridPos.y));
    }
    public Grid GetGrid(int gridX, int gridY)
    {
        if (gridX < 0 || gridY < 0) { return null; }
        if (gridX >= _colum || gridY >= _row) { return null; }

        return _allGrids[gridX][gridY];
    }

    // 获取格子中心坐标(左下角是0，0点)。
    public Vector2 GetGridCenter(Vector3 worldPos)
    {
        return GetGridCenter(GetGridPos(worldPos));
    }
    // 获取格子中心坐标
    public Vector2 GetGridCenter(Vector2 gridPos)
    {
        return GetGridCenter(gridPos.x, gridPos.y);
    }
    public Vector2 GetGridCenter(float x, float y)
    {
        return _leftBottom + new Vector3(x * _gridWidth + _halfWidth, y * _gridHeight + _halfHeight);
    }

    private void onGizmos()
    {
        if (_leftBottom == Vector3.zero || _gridWidth == 0f) { return; }

        // 竖线
        for (int x = 0; x < _colum; ++x)
        {
            var bottom = new Vector3(_leftBottom.x + _gridWidth * x, _leftBottom.y, _leftBottom.z);
            Gizmos.DrawLine(bottom, bottom + new Vector3(0, _gridHeight * _row, 0));
        }

        // 横线
        for (int y = 0; y < _row; ++y)
        {
            var left = new Vector3(_leftBottom.x, _leftBottom.y + _gridHeight * y, _leftBottom.z);
            Gizmos.DrawLine(left, left + new Vector3(_gridWidth * _colum, 0, 0));
        }
    }
}

